<template>
	<view class="goods_list">
		<my-goods_item v-for="(item,index) in goodsList" :key="index" :goods="item" @click.native="goToDetail(item.goods_id)"></my-goods_item>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsQuery: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				flag: true
			};
		},
		onLoad(option) {
			this.goodsQuery = Object.assign(this.goodsQuery, option)
			this.getGoodsList()
		},
		onReachBottom() {
			this.goodsQuery.pagenum++
			if (this.goodsQuery.pagenum * this.goodsQuery.pagesize >= this.total) return uni.$showMsg('没有更多数据了')
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.goodsQuery = {
				query: '',
				cid: '',
				pagenum: 1,
				pagesize: 10
			}
			this.goodsList = []
			this.getGoodsList(() => {
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			goToDetail(id) {
				uni.navigateTo({
					url:`/subpkg/goods_detail/goods_detail?goods_id=${id}`
				})
			},
			async getGoodsList(fn) {
				if (this.flag) {
					this.flag = false
					let {
						data
					} = await uni.$http.get('/api/public/v1/goods/search', this.goodsQuery)
					if (data.meta.status !== 200) return uni.$showMsg
					this.goodsList = [...this.goodsList, ...data.message.goods]
					this.total = data.message.total
					this.flag = true
					fn && fn()
				}
			}
		}
	}
</script>

<style lang="scss">

</style>